/**
 * 报警综合分析
 */
$(function(){
	AlarmSynAnalysis.init();
});

var queryType = 0;
var alarmType = "";
var AlarmSynAnalysis = {
	param: {},
	init: function(){
		var me = this;
		me.myChart=null;
		me.initQueryTime();
		me.barChart = me.initBarChart();
		me.lineChart = me.initLineChart();
		me.initTableTitle();
		me.initEvent();
		//加载平台下拉框
		me.loadPlatformAlarm();
		//加载业户下拉框
		me.loadEnterpriseAlarm();
		//加载车辆下拉框
		me.loadVehicleAlarm();
		//加载司机下拉框
		me.loadDriverAlarm();
		//默认查询
		$("#queryBtn").trigger("click");
	},
	getToday : function(){
		var todayDate = new Date();
        var yyyy = todayDate.getFullYear();
        var mm = todayDate.getMonth()+1;
        var dd = todayDate.getDate();  
        if (mm.toString().length == 1) {
	        mm = '0' + mm;
	    }
	    if (dd.toString().length == 1) {
	        dd = '0' + dd;
	    }
        return yyyy + '-' + mm + '-' + dd;
	},
    initQueryTime:function(){
        var todayDate = new Date();
    
       
        if(flag){        
        	var todayDateStr = AlarmSynAnalysis.getToday();
        	$("#inputStartDate").val(todayDateStr);
   	        $("#inputEndDate").val(todayDateStr);
        }else{	
	        var beforeDate = new Date(todayDate-86400000*1); //前一天
		    var yyyy = beforeDate.getFullYear(), mm = (beforeDate.getMonth() + 1).toString(), dd = beforeDate.getDate().toString();
		    if (mm.length == 1) {
		        mm = '0' + mm;
		    }
		    if (dd.length == 1) {
		        dd = '0' + dd;
		    }
	        $("#inputStartDate").val(yyyy + '-' + mm + '-' + dd);
	        $("#inputEndDate").val(yyyy + '-' + mm + '-' + dd);
        }
			
    },
	initEvent : function(){
	    	$("#queryType").val("plat");
	    	$('.tabsHead').on('click','li',function(){
				var index = $(this).index();
				$('.tabsHead').find('li').removeClass('active');
				$(this).addClass('active');
				queryType = index;
				if (index==0) {
					$("#queryPlatformSel").next().show();
					$("#queryEnterpriseSel").next().hide();
					$("#queryVehicleSel").parent().hide();
					$("#queryDriverSel").next().hide();
				}else if (index==1) {
					$("#queryPlatformSel").next().hide();
					$("#queryEnterpriseSel").next().show();
					$("#queryVehicleSel").parent().hide();
					$("#queryDriverSel").next().hide();
				}else if (index==2) {
					$("#queryPlatformSel").next().hide();
					$("#queryEnterpriseSel").next().hide();
					$("#queryVehicleSel").parent().show();
					$("#queryDriverSel").next().hide();
				}else if (index==3) {
					$("#queryPlatformSel").next().hide();
					$("#queryEnterpriseSel").next().hide();
					$("#queryVehicleSel").parent().hide();
					$("#queryDriverSel").next().show();
				}
			});
	    	$("#queryBtn").on('click',function(){
	    		var inputStartDate = $("#inputStartDate").val();
	    		var inputEndDate = $("#inputEndDate").val();
	    		var sta_date = new Date(inputStartDate);
	            var end_date = new Date(inputEndDate);//将字符串转化为时间
	            var num = (end_date - sta_date) / (1000 * 3600 * 24);//求出两个时间的时间差，这个是天数
	            var days = parseInt(Math.ceil(num));//转化为整天（小于零的话剧不用转了）
	            if(num<0){
	        		global.ajaxLoading.pop("查询开始时间不能晚于结束时间",false,2000);
	        		return;
	        	}
	            if (end_date.getMonth()-sta_date.getMonth()>1) {
	            	global.ajaxLoading.pop("查询时间不能超过一个月",false,2000);
	                return;
				}
	            if (days > 31) {
	                global.ajaxLoading.pop("查询时间不能超过一个月", false, 2000);
	                return;
	            }
	    		var today = AlarmSynAnalysis.getToday();
	    		if(inputStartDate == inputEndDate && inputStartDate == today){
	    			$("#chartDiv").hide();
	    			$("#chartMillage2 .bDiv").height(nav.whiceH[0]-270+'px');
	    			$('.chart').css('border','none');
	    			$('.chart .detail-report').css('margin-top','5px')
	    		}else{
	    			$("#chartDiv").show();
	    		}
	    		AlarmSynAnalysis.getChartData();
	        	AlarmSynAnalysis.tableReload();
	    	});
	    	$("#exportExcel").on('click',function(){
	    		AlarmSynAnalysis.exportSimCard();
	    	});
	        $("#confirmExport").click(function () {
	        	AlarmSynAnalysis.exportComfirm();
	        })
	     	$("img[name='trackPlayBtn']").bind('click',function(){});
	    },
	    // 获取查询条件
	    getParams: function () {
	    	var queryContent;
	    	if(queryType == 0){
	    		queryContent = $("#queryPlatformSel").val();
	    	}else if(queryType == 1){
	    		queryContent = $("#queryEnterpriseSel").val();
	    	}else if(queryType == 2){
	    		queryContent = $.trim($("#queryVehicleSel").val());
	    	}else if(queryType == 3){
	    		queryContent = $("#queryDriverSel").val();
	    	}
	        
	        var startDate = $("#inputStartDate").val() + " 00:00:00";
	        var endDate = $("#inputEndDate").val() + " 23:59:59";
	        var params = [{
	        	name: 'alarmType',
	            value: alarmType
	        }, {
	        	name: 'queryType',
	        	value: queryType
	        }, {
	            name: 'queryContent',
	            value: queryContent
	        }, {
	            name: 'startDate',
	            value: startDate
	        }, {
	            name: 'endDate',
	            value: endDate
	        }];
	        return params;
	    },

	    //查询柱状图和折线图数据
	    getChartData :function(){
	    	var _this = this;
	    	AlarmSynAnalysis.clearChart();
	    	$.ajax({
	    		type: 'post',
	    		data : AlarmSynAnalysis.getParams(),
	    		url: appCtx + "/report/alarmSyn/getChartData.html?time="+ new Date().getTime(),
	    		dataType: "JSON",
	    		success: function (data) {
	    			if(data){
	    				var barList = data.barList;
	    				var lineList = data.lineList;
	    				if(barList.length > 0){
	    					var overspeed_count = barList[0].overspeed_count;					//超速报警
	    					var roadoffset_count = barList[0].roadoffset_count;				//路线偏离报警
	    					var tired_count = barList[0].tired_count;							//疲劳驾驶报警
	    					//var nightly_3rd_count = barList[0].nightly_3rd_count;				//2-5点禁行报警
	    					var prohibit_driving_count = barList[0].prohibit_driving_count;	//夜间三级路面报警
	    					_this.barChart.option.series[0].data = [overspeed_count,roadoffset_count,tired_count,prohibit_driving_count];
	    					_this.barChart.refash(_this.barChart.option);
	    				}
	    				if(lineList.length > 0){
	    					var xAxis = [];
	    					var series = [];
	    					$.each(lineList,function(index, obj) {
	    						var overspeed_count = parseInt(obj.overspeed_count);					//超速报警
	    						var roadoffset_count = parseInt(obj.roadoffset_count);				//路线偏离报警
	    						var tired_count = parseInt(obj.tired_count);							//疲劳驾驶报警
	    						//var nightly_3rd_count = parseInt(obj.nightly_3rd_count);				//2-5点禁行报警
	    						var prohibit_driving_count = parseInt(obj.prohibit_driving_count);	//夜间三级路面报警
	    						
	    						var month = AlarmSynAnalysis.getFormatDate(obj._id);		//月份
	    						var alarmCount = overspeed_count + roadoffset_count + tired_count + prohibit_driving_count;
	    						
	    						xAxis.push(month);
	    						series.push(alarmCount);
	    					});
	    					
	    					_this.lineChart.option.xAxis[0].data = xAxis;
	    					_this.lineChart.option.series[0].data = series;
	    					_this.lineChart.refash(_this.lineChart.option);
	    				}
	    			}
	    		}
	    	});
	    },
	  //点击柱状图关联折线图数据
	    getLineDataByBar :function(){
	    	var _this = this;
	    	AlarmSynAnalysis.clearLineChart();
	    	$.ajax({
	    		type: 'post',
	    		data : AlarmSynAnalysis.getParams(),
	    		url: appCtx + "/report/alarmSyn/getLineChartData.html?time="+ new Date().getTime(),
	    		dataType: "JSON",
	    		success: function (data) {	
	    			if(data.length > 0){
	    				var xAxis = [];
	    				var series = [];
	    				$.each(data,function(index, obj) {
	    					var month = AlarmSynAnalysis.getFormatDate(obj._id);		//月份
	    					xAxis.push(month);
	    					if(alarmType == "overspeed"){
	    						var overspeed_count = parseInt(obj.overspeed_count);					//超速报警
	    						series.push(overspeed_count);
	    					}else if(alarmType == "roadoffset"){
	    						var roadoffset_count = parseInt(obj.roadoffset_count);				//路线偏离报警
	    						series.push(roadoffset_count);
	    					}else if(alarmType == "tired"){
	    						var tired_count = parseInt(obj.tired_count);							//疲劳驾驶报警
	    						series.push(tired_count);
	    					}else if(alarmType == "prohibit_driving"){
	    						var prohibit_driving_count = parseInt(obj.prohibit_driving_count);	//夜间三级路面报警
	    						series.push(prohibit_driving_count);
	    					}
	                    });
	    				
	    				_this.lineChart.option.xAxis[0].data = xAxis;
	    				_this.lineChart.option.series[0].data = series;
	    				_this.lineChart.refash(_this.lineChart.option);
	    			}
	    		}
	    	});
	    },
	  //点击柱状图关联表格数据
	    getTableDataByBar :function(){
	    	var _this = this;
	    	AlarmSynAnalysis.clearLineChart();
	    	$.ajax({
	    		type: 'post',
	    		data : AlarmSynAnalysis.getParams(),
	    		url: appCtx + "/report/alarmSyn/getTableChartData.html?time="+ new Date().getTime(),
	    		dataType: "JSON",
	    		success: function (data) {	
	    			if(data.length > 0){
	    				var xAxis = [];
	    				var series = [];
	    				$.each(data,function(index, obj) {
	    					var month = AlarmSynAnalysis.getFormatDate(obj._id);		//月份
	    					xAxis.push(month);
	    					if(alarmType == "overspeed"){
	    						var overspeed_count = parseInt(obj.overspeed_count);					//超速报警
	    						series.push(overspeed_count);
	    					}else if(alarmType == "roadoffset"){
	    						var roadoffset_count = parseInt(obj.roadoffset_count);				//路线偏离报警
	    						series.push(roadoffset_count);
	    					}else if(alarmType == "tired"){
	    						var tired_count = parseInt(obj.tired_count);							//疲劳驾驶报警
	    						series.push(tired_count);
	    					}else if(alarmType == "prohibit_driving"){
	    						var prohibit_driving_count = parseInt(obj.prohibit_driving_count);	//夜间三级路面报警
	    						series.push(prohibit_driving_count);
	    					}
	                    });
	    				
	    				_this.lineChart.option.xAxis[0].data = xAxis;
	    				_this.lineChart.option.series[0].data = series;
	    				_this.lineChart.refash(_this.lineChart.option);
	    			}
	    		}
	    	});
	    },
	    //车辆下拉框
	    loadVehicleAlarm :function(){
	    	$("#queryVehicleSel").select2({
		        theme: "default",
		        allowClear: true,
		        placeholder: "全部车辆",
		        ajax:{
		            url:appCtx + "/system/common/selectVehicle.html",
		            dataType:"json",
		            delay:250,
		            data:function(params){
		                return {
		                    name: params.term,
		                    page: params.page || 1
		                };
		            },
		            cache:true,
		            processResults: function (res, params) {
				    	if(res){
				    		var options = [];
				    		var option = {"id":" ", "text":"全部车辆"};
				    		options.push(option);
		    				$.each(res,function(index,obj) {
		    					var option = {"id":obj.vehicle_no, "text":obj.vehicle_no};
	    	                    options.push(option);
		    				});
	    	                return {
	    	                	results: options,
	    	                    pagination: {
	    	                        more:false
	    	                    }
	    	                };
				    	}
		            },
		            escapeMarkup: function (markup) { return markup; },
		            minimumInputLength: 1
		        }
		    });
	    	//默认隐藏
			$("#queryVehicleSel").parent().hide();
//				$.ajax({
//		            type: 'post',
//		            url: appCtx + "/report/alarmSyn/queryVehicleListByRole.html?time="+ new Date().getTime(),
//		            dataType: "JSON",
//		            success: function (data) {
//		            	$('#queryVehicleSel').html("");
//		    			if(data){
//		    				var selectStr="";
//		    				selectStr+="<option value=''>全部车辆</option>";
//		    				$.each(data,function(entryIndex,entry) {
//		    					selectStr+="<option value='"+entry.vehicle_no+"'>"+entry.vehicle_no+"</option>";
//		    				});
//		    				$('#queryVehicleSel').html(selectStr);
//		    				$("#queryVehicleSel").select2();
//		    				//默认隐藏
//		    				$("#queryVehicleSel").next().hide();
//		    			}
//		            }
//		        });
		},
	    //司机下拉框
	    loadDriverAlarm :function(){
	    	$('#queryDriverSel').select2({
	    		theme: "default",
	 	        allowClear: true,
	 	        placeholder: "全部司机",
	 	        ajax:{
	 	            url:appCtx + "/report/alarmSyn/loadDriverListByRole.html?time="+ new Date().getTime(),
	 	            dataType:"json",
	 	            delay:250,
	 	           async : true,
	 	            data:function(params){
	 	                return {
	 	                    name: params.term,
	 	                    page: params.page || 1
	 	                };
	 	            },
	 	            cache:true,
	 	            processResults: function (res, params) {
	 			    	if(res){
	 			    		var options = [];
	 			    		var option = {"id":" ", "text":"全部司机"};
	 			    		options.push(option);
	 	    				$.each(res,function(index,obj) {
	 	    					var option = {"id":obj.id, "text":obj.name};
	     	                    options.push(option);
	 	    				});
	     	                return {
	     	                	results: options,
	     	                    pagination: {
	     	                        more:false
	     	                    }
	     	                };
	 			    	}
	 	            },
	 	            escapeMarkup: function (markup) { return markup; },
	 	            minimumInputLength: 1
	 	           
	 	        }
	    		
	    	})
	    	$("#queryDriverSel").next().hide();
				/*$.ajax({
		            type: 'post',
		            url: appCtx + "/report/alarmSyn/loadDriverListByRole.html?time="+ new Date().getTime(),
		            dataType: "JSON",
		            success: function (data) {
		            	$('#queryDriverSel').html("");
		    			if(data){
		    				var selectStr="";
		    				selectStr+="<option value=''>所有司机</option>";
		    				$.each(data,function(entryIndex,entry) {
		    					selectStr+="<option value='"+entry.licence+"'>"+entry.name+"</option>";
		    				});
		    				$('#queryDriverSel').html(selectStr);
		    				$("#queryDriverSel").select2();
		    				//默认隐藏
		    				$("#queryDriverSel").next().hide();
		    			}
		            }
		        });*/
		},
		//加载平台和业户下拉框
		loadPlatformAlarm:function(){
			$('#queryPlatformSel').select2({
				  theme: "default",
			        allowClear: true,
			        placeholder: "全部平台",
			        ajax:{
			            url:appCtx + "/report/alarmSyn/queryEntAndPlatByRole.html?time="+ new Date().getTime()+"&plat=9999",
			            dataType:"json",
			            delay:250,
			            data:function(params){
			                return {
			                    name: params.term,
			                    page: params.page || 1
			                };
			            },
			            cache:true,
			            processResults: function (res, params) {
					    	if(res){
					    		var options = [];
					    		var option = {"id":" ", "text":"全部平台"};
					    		options.push(option);
			    				$.each(res,function(index,obj) {
			    					var option = {"id":obj.platformCode, "text":obj.platName};
		    	                    options.push(option);
			    					
//			    						selectStr+="<option value='"+entry.platformCode+"'>"+entry.platName+"</option>";
//			    						platArray.push(entry.platformCode);
//			    					}
			    					
			    				});
		    	                return {
		    	                	results: options,
		    	                    pagination: {
		    	                        more:false
		    	                    }
		    	                };
					    	}
			            },
			            escapeMarkup: function (markup) { return markup; },
			            minimumInputLength: 1
			        }
			})
			$("#queryEnterpriseSel").next().hide();
		},
		
		
		loadEnterpriseAlarm :function(){
			$('#queryEnterpriseSel').select2({
	    		theme: "default",
	 	        allowClear: true,
	 	        placeholder: "全部业户",
	 	        ajax:{
	 	            url:appCtx + "/report/alarmSyn/queryEntAndPlatByRole.html?time="+ new Date().getTime(),
	 	            dataType:"json",
	 	            delay:250,
	 	           async : true,
	 	            data:function(params){
	 	                return {
	 	                    name: params.term,
	 	                    page: params.page || 1
	 	                };
	 	            },
	 	            cache:true,
	 	            processResults: function (res, params) {
	 	          
	 			    	if(res){
	 			    		var options = [];
	 			    		var option = {"id":" ", "text":"全部业户"};
	 			    		options.push(option);
	 	    				$.each(res,function(index,obj) {
	 	    					var option = {"id":obj.entId, "text":obj.entName};
	     	                    options.push(option);
	 	    				});
	     	                return {
	     	                	results: options,
	     	                    pagination: {
	     	                        more:false
	     	                    }
	     	                };
	 			    	}
	 	            },
	 	            escapeMarkup: function (markup) { return markup; },
	 	            minimumInputLength: 1
	 	        }
			
	    	});
//			
//			 $.ajax({
//	            type: 'post',
//	            url: appCtx + "/report/alarmSyn/queryEntAndPlatByRole.html?time="+ new Date().getTime(),
//	           
//	            dataType: "JSON",
//	            success: function (data) {	
//	            	$('#queryPlatformSel').html("");
//	    			if(data){
//	    				var selectStr="";
//	    				selectStr+="<option value=''>全部平台</option>";
//	    				
//	    				var platArray = new Array();
//	    				$.each(data,function(entryIndex,entry) {
//	    					if(jQuery.inArray(entry.platformCode, platArray) == -1){
//	    						selectStr+="<option value='"+entry.platformCode+"'>"+entry.platName+"</option>";
//	    						platArray.push(entry.platformCode);
//	    					}
//	    				});
//	    				$('#queryPlatformSel').html(selectStr);
//	    				$("#queryPlatformSel").select2();
//	    				
//	    				var selectStrs="";
//	    				selectStrs+="<option value=''>全部业户</option>";
//	    				$.each(data,function(entryIndex,entry) {
//	    						  selectStrs+="<option value='"+entry.entId+"'>"+entry.entName+"</option>";
//	    				});
//	    				$('#queryEnterpriseSel').html(selectStrs);
//	    				$("#queryEnterpriseSel").select2();
//	    				//默认隐藏
//	    				$("#queryEnterpriseSel").next().hide();
//	    				$("#queryEnterpriseSel").next().css("border","none");
//	    				$("#queryPlatformSel").next().css("border","none");
//	    			}
//	            }
//	        });
			$("#queryEnterpriseSel").next().hide();
		},
		getFormatDate : function (msg){
		   var outTime = "0";
		   if (msg != "" && null != msg && undefined != msg ) {
	  		var date = new Date(parseInt(msg)); 
		    var y = date.getFullYear();    
		    var m = date.getMonth() + 1;    
		    m = m < 10 ? ('0' + m) : m;    
		    var d = date.getDate();    
		    d = d < 10 ? ('0' + d) : d;    
		    var h = date.getHours();  
		    h = h < 10 ? ('0' + h) : h;  
		    var minute = date.getMinutes();  
		    var second = date.getSeconds();  
		    minute = minute < 10 ? ('0' + minute) : minute;    
		    second = second < 10 ? ('0' + second) : second;   
		    outTime = y + '-' + m + '-' + d;	//不统计时分秒
	  	  	}
		  	return outTime;
		},
		clearChart : function(){
			var _this = this;
	    	//查询之前先清空数据
	    	_this.barChart.option.series[0].data = [0,0,0,0,0];
			_this.barChart.refash(_this.barChart.option);
			AlarmSynAnalysis.clearLineChart();
		},
		clearLineChart : function(){
			var _this = this;
			//查询之前先清空数据
			_this.lineChart.option.xAxis[0].data = [];
			_this.lineChart.option.series[0].data = [];
			_this.lineChart.refash(_this.lineChart.option);
		},
	initBarChart: function(){
		var me = this;
		//计算显示的柱子数
		var percent = 100;

		option = {
		    color: ['#4F81BD'],
		    legend: {
                data:['报警车辆数','处警率']
            },
		    tooltip : {
		        trigger: 'axis',
		        show: true,
		        axisPointer : {            // 坐标轴指示器，坐标轴触发有效
		        	show: false,
		            type : 'none'        // 默认为直线，可选为：'line' | 'shadow'
		        },
			        formatter: function (params) {
			            params = params[0];
			            return "报警类型:"+params.name+"<br/>"+"报警数:"+params.value+"次";
			       },
		        },
		    grid: {
		        left: '3px',
		        top:'10%',
		        right: '10px',
		        bottom:'10px',
		        containLabel: true
		    },
		    xAxis : [
		        {
		            type : 'category',
		            data : ['超速', '路线偏离', '疲劳驾驶', '2-5点禁行'],
		            axisTick: {
		                alignWithLabel: true
		            },
		            axisLabel:{
		            	textStyle: {
                            fontSize: 10
                        }
		            }
		        }
		    ],
		    yAxis : [
		        {
		            type : 'value',
		            show: true,    //显示纵轴false-不显示，true-显示
		            name: '报警数'
		        }
		    ],
		    series : [
		        {
		            name:'',
		            type:'bar',
		            barWidth: '40%',
		            data:[0,0,0,0]
		        }
		    ],
		};
            var myChart = echarts.init(document.getElementById('barChart'));
            myChart.setOption(option);
            window.onresize = myChart.resize;
            
            myChart.on('click', function(param){
            	if(param.dataIndex == 0){
            		alarmType = "overspeed";
            	}else if(param.dataIndex == 1){
            		alarmType = "roadoffset";
            	}else if(param.dataIndex == 2){
            		alarmType = "tired";
            	}else if(param.dataIndex == 3){
            		alarmType = "prohibit_driving";
            	}
            	AlarmSynAnalysis.getLineDataByBar();
            	
            	AlarmSynAnalysis.tableReload();
            });
            
            return {
            	refash : function(option){
                    myChart.setOption(option);
            	},
            	option : option,
            	barChartDom : myChart
            };        
	},
	
	
	
	initLineChart: function(){
		var me = this;
		//计算显示的柱子数
		var percent = 100;

		option = {
		    color: ['#4F81BD'],
		    legend: {
                data:['报警数']
            },
		    tooltip : {
		        trigger: 'axis',
		        show: true,
		        axisPointer : {            // 坐标轴指示器，坐标轴触发有效
		        	show: false,
		            type : 'none'        // 默认为直线，可选为：'line' | 'shadow'
		        },
		        formatter:function(a){
		        	if(a[0].name==''){
		        		return;
		        	}
		        	var relVal = "";
		        	relVal = a[0].name+"："+a[0].value+"次";
		        	return relVal;
		        }
		    },
		    grid: {
		        left: '3px',
		        top:'10%',
		        right: '10px',
		        bottom:'10px',
		        containLabel: true
		    },
		    xAxis : [
		        {
		            type : 'category',
		            data : [],
		            axisTick: {
		                alignWithLabel: true
		            },
		            axisLabel:{
		            	textStyle: {
                            fontSize: 10
                        }
		            }
		        }
		    ],
		    yAxis : [
		        {
		            type : 'value',
		            show: true,    //显示纵轴false-不显示，true-显示
		            name: '报警数'
		        }
		    ],
		    series : [
		        {
		            name:'收费站统计次数',
		            type:'line',
		            barWidth: '60%',
		            data:[]
		        }
		    ],
		};
            var myChart = echarts.init(document.getElementById('lineChart'));
            myChart.setOption(option);
            window.onresize = myChart.resize;
            return {
            	refash : function(option){
                    myChart.setOption(option);
            	},
            	option : option,
            	barChartDom : myChart
            };        
	},
    initTableTitle:function(){
        var getVehicleColor = function(vehicle_color){
        	return global.carColor(vehicle_color);
        };
        var getAlarm_src = function(alarm_src){
        	if(alarm_src == 1){
        		return "终端";
        	}else if(alarm_src == 2){
        		return "企业平台";
        	}
        };
        var getFormatTime = function (msg){
	  	    if (msg != "" && null != msg && undefined != msg ) {
	  	    	var outTime = "0";
		  		var date = new Date(parseInt(msg)); 
	  		    var y = date.getFullYear();    
	  		    var m = date.getMonth() + 1;    
	  		    m = m < 10 ? ('0' + m) : m;    
	  		    var d = date.getDate();    
	  		    d = d < 10 ? ('0' + d) : d;    
	  		    var h = date.getHours();  
	  		    h = h < 10 ? ('0' + h) : h;  
	  		    var minute = date.getMinutes();  
	  		    var second = date.getSeconds();  
	  		    minute = minute < 10 ? ('0' + minute) : minute;    
	  		    second = second < 10 ? ('0' + second) : second;   
	  		    outTime = y + '-' + m + '-' + d+'  '+h+':'+minute+':'+second;
	  		    return outTime;
		  	}else{
		  		return "";
		  	}
		 };
		 
		 var getAlarmPosition = function(vehicle_no,vehicle_color,alarm_time,lng,lat){
			 if(lng == undefined || lng == "undefined"){
				 lng = 0;
			 }
			 if(lat == undefined || lat == "undefined"){
				 lat = 0;
			 }
			 var vehicle_no_color = encodeURI(vehicle_no + "_" + vehicle_color);
			 alarm_time = new Date(alarm_time.replace(/-/g,"/")).getTime();
			 return "<a target='_blank' href='" + appCtx + "/report/alarmSyn/toVehiclePosition.html?vehicle_no_color=" + vehicle_no_color + "&lng=" + lng + "&lat=" + lat + "&alarm_time=" + alarm_time + "'>查看地图</a>";
		 };
		 
		 var toDriver = function(licence,driverName){
			 return "<a target='_blank' href='" + appCtx + "/base/PubDriver/toAlarmDriver.html?licence=" + licence + "'>" + driverName + "</a>";
		 };
		 
		/* var getAdasMaterial=function(vehicle_no,alarm_time,alarm_type_id){
			 if(AlarmSynAnalysis.isAdasAlarm(alarm_type_id)){
				 return "<a href=\"javascript:AlarmSynAnalysis.getAdasMaterialData('"+vehicle_no+"','"+alarm_time+"');\">查看证据</a>"
			 }
			 return "";
			 
		 };*/
		 
    	$("#objTable").flexigrid({
	            dataType : 'json',
	            colModel : [{
	                display : '车牌号',
	                name : 'vehicle_no',
	                width : 8,
	                sortable:false,
	                showTitle:true,
	                align : 'left'
	            },{
	                display : '颜色',
	                width : 4,
	                name : 'vehicle_color',
	                align : 'left',
	                sortable:false,
	                paramcolnames:['vehicle_color'],
	                handlefunction : getVehicleColor
	            },{
	                display : '司机姓名',
	                name : 'driverName',
	                width : 6,
	                align : 'left',
	                sortable:false,
	                paramcolnames:['licence','driverName'],
	                handlefunction : toDriver
	            },{
	            	display : '业户名称',
	            	name : 'enterprise_name',
	            	width : 12,
	            	align : 'left',
	            	showTitle:true,
	            	sortable:false
	            },{
	                display : '平台名称',
	                name : 'platform_name',
	                width : 12,
	                align : 'left',
	                showTitle:true,
	                sortable:false,
	            },{
	            	display : '报警类型',
	            	name : 'alarm_type',
	            	width : 8,
	            	align : 'left',
	            	sortable:false
	            },{
	                display : '报警来源',
	                name : 'alarm_src',
	                width : 8,
	                align : 'left',
	                sortable:false,
	                paramcolnames:['alarm_src'],
	                handlefunction : getAlarm_src
	            },{
	                display : '报警时间',
	                name : 'alarm_time',
	                width : 10,
	                align : 'left',
	                sortable:false,
	                /*paramcolnames:['alarm_time'],
	                handlefunction : getFormatTime*/
	            },{
	                display : '处理时间',
	                name : 'handle_time',
	                width : 10,
	                align : 'left',
	                sortable:false,
	                paramcolnames:['handle_time'],
	                handlefunction : getFormatTime
	            },{
	                display : '处理结果',
	                name : 'handle_result',
	                width : 8,
	                align : 'left',
	                sortable:false
	            },{
	                display : '报警位置',
	                name : 'vehicle_no',
	                width : 7,
	                align : 'left',
	                sortable:false,
	                paramcolnames:['vehicle_no','vehicle_color','alarm_time','lng','lat'],
	                handlefunction : getAlarmPosition
	            /*},{
	                display : '安全报警取证',
	                name : 'vehicle_no',
	                width : 7,
	                align : 'left',
	                sortable:false,
	                paramcolnames:['vehicle_no','alarm_time','alarm_type_id'],
	                handlefunction : getAdasMaterial*/
	            }] ,
	            usepager : true,// 是否分页，默认为true。
	            resizable : false,
				sortname : "id",// 第一次加载数据时排序列
				sortorder : "desc",// 第一次加载数据时排序类型
				usepager : true,// 是否分页，默认为true
				minheight: 50, //列的最小高度
				nowrap: true, //是否不换行
				showToggleBtn : false,
				showTableToggleBtn : true,// 是否显示收起/打开按钮,默认不显示。
				useRp : true,// 是否可以动态设置每页显示的结果数，默认为false。
				rp : 10,// 每页记录数，默认为10
				rpOptions: [10, 20, 50, 100], // 每页结果数
				checkbox : false,// 是否要多选框,默认为false。
				rowId : 'id',// 多选框绑定行的id,只有checkbox : true时才有效。
	            height:$(window).height() - 240,
	            width:$('.detail-report').width(),
	            widthType:"%"
	         });	 
    },
	tableReload: function(){
		$("#objTable").flexOptions({
			newp :1,// 设置起始页
			url : appCtx + "/report/alarmSyn/getTableChartData.html",
			params : AlarmSynAnalysis.getParams()
		}).flexReload();
	},
    exportExcel : function(){
    	var me = this;
		var header1 = JSON.stringify(me.getColumn());
		var header2 = JSON.parse(header1);
		
		var headerWidth = $('.detail-report').width();
		for(var i=0;i<header2.length;i++){
			header2[i].width = Math.ceil(headerWidth / 100 * header2[i].width);
		}
		header2.splice(header2.length-1,1);
		var header3 = JSON.stringify(header2);
		 var imageUrl = me.barChart.barChartDom.getDataURL({
			type: 'png',
		    pixelRatio: 1
		}); 
		 
		 var imageUrl2 = me.pieChart.pieChartDom.getDataURL({
				type: 'png',
			    pixelRatio: 1
		}); 
		 
		//表头
		var colModelList=new Array();
		colModelList.push(header3);
		var colModel= $.toJSON(colModelList);
		
		//图片
		var imgsrcList=new Array();
		imgsrcList.push(imageUrl);
		imgsrcList.push(imageUrl2);
		var imgsrc= $.toJSON(imgsrcList);
			
		var param =  {
				startTime:$("#inputStartDate").val()+" 00:00:00",
				endTime: $("#inputEndDate").val()+" 23:59:59",
				vehicleId:$("#selectVehicleSel").val(),
				unitId:$("#selectVehicleUnitSel").val(),
				colModel:colModel,
				title:"报警分析报表",
				imgsrc:imgsrc
		};
		post(appCtx+"report/nightDriving/exportNightDriving.html",param);
		
	},
	getAdasMaterialData:function(vehicle_no,alarm_time){
		
			$.ajax({
	    		type: 'post',
	    		url: appCtx + "/report/alarmSyn/getAdasMaterial.html?vehicle_no="+vehicle_no+"&alarm_time="+alarm_time,
	    		dataType: "JSON",
	    		success: function (data) {
	    			if (data) {
        		   		var dataJson = JSON.parse(data);
						var imgUrl = dataJson.imgUrl;
						var isHave = dataJson.isHave;
						if (parseInt(isHave)==1) {
							if (parseInt(dataJson.mediaFormat) == 0) {
								$("#modalTitle").html("图片展示");
								$("#msg").hide();
								$("#largeImgage").show();
								$("#largeImgage").attr("src",imgUrl);
								$("#pictureModal").modal('show');
							}else if (parseInt(dataJson.mediaFormat) == 5){
								$("#showVedio").attr("src",imgUrl);
								$("#vedioModal").modal('show');
							}
						}else{
							$("#largeImgage").hide();
							$("#modalTitle").html("取证展示");
							$("#msg").show();
							$("#pictureModal").modal('show');
						}
        		   }
	    		}
	    	});
	},
	getAdasAlarmType:function(){
		var map={};
		map[100] = "前车报警"; 
		map[101] = "左变道报警";
		map[102] = "右变道报警";
		map[103] = "行人报警";
		map[104] = "闭眼 ";
		map[105] = "打哈欠";
		map[106] = "打电话";
		map[107] = "吸烟";
		map[108] = "姿态异常";
		map[109] = "无驾驶员";
		map[110] = "急加速";
		map[111] = "急减速";
		map[112] = "急转弯";
		map[113] = "酒驾";
		map[114] = "紧急刹车";
		return map;
	},
	 isAdasAlarm:function(alarm_type_id){
		var map=AlarmSynAnalysis.getAdasAlarmType();
		if(map[alarm_type_id]!=null){
			return true;
		}
		return false;
	}
	
};
